package com.qding.park.manager.service.core.biz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.manager.adapter.factory.AdapterParkProxyFactory;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.pojo.TbParkingrule;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.ap.ApParkTypeInfo;
import com.qding.park.manager.pojo.enums.RecordStatusEnum;
import com.qding.park.manager.service.core.mapper.TbParkingruleMapper;

@Component("confDateBiz")
public class ConfDateBiz {

    private static final Logger _log = LoggerFactory.getLogger(ConfDateBiz.class);

    @Autowired
    private ParkCacheInter parkCacheInter;

    @Autowired
    private TbParkingruleMapper tbParkingruleMapper;

    /**
     * @Field @parkBiz : TODO(车场业务)
     */
    @Autowired
    private ParkBiz parkBiz;

    /**
     * @Field @adapterParkProxyFactory : TODO(底层车场适配器代理工厂)
     */
    @Autowired
    private AdapterParkProxyFactory adapterParkProxyFactory;

    public void parkRuleSync(int parkId) throws BaseException {
        // // 查询所有的车场列表
        // List<TbParkspace> parkSpaceList = tbParkspaceMapper.listAll();
        //
        // if (ListUtil.isEmpty(parkSpaceList)) {
        // _log.info("车场列表为空，同步任务已完成");
        // return;
        // }

        // TbParkspace park = tbParkspaceMapper.queryParkInfoByParkId(parkId);
        TbParkspace park = parkCacheInter.queryParkSpace(parkId);

        if (park == null) {
            _log.info("车场列表为空，同步任务已完成");
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getDesc(), "无此车场", " 车牌=" + parkId));
        }

        // 车场编号
        String parkCode = park.getParkCode();
        // 车场软件供应商编号
        int supplierId = park.getSupplierId();

//        AdapterProxy adapterProxy = adapterParkProxyFactory.createAdapterParkProxyBySupplierId(supplierId);
//
//        List<ApParkTypeInfo> apParkTypeList = adapterProxy.getRuleAdapter().AccountRuleByParkId(parkCode);
        
        
        
        List<ApParkTypeInfo> apParkTypeList =null;

        List<TbParkingrule> tbParkRulelist = new ArrayList<TbParkingrule>();

        if (ListUtil.isEmpty(apParkTypeList)) {
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NO_DATA_FIND.getDesc(), "此车场没有规则数据",
                            " 车牌=" + parkId));
        }

        for (ApParkTypeInfo aprule : apParkTypeList) {
            // 规则编码 与busyName对应
            String busyID = aprule.getBusyID();
            // 时段类型名称 (0分时段 1高峰时段 2非高峰时段 3非工作日)
            String busyName = aprule.getBusyName();
            // 表示车辆类型 (0摩托车1小型车2大型车3超大型车) 公共部分
            int carTypeCode = aprule.getCarTypeCode();
            // 时段结束时间00:00
            String duringEnd = aprule.getDuringEnd();
            // 时段内费用上限(单位分)
            int duringHighestFee = aprule.getDuringHighestFee();
            // 时段开始时间 00:00
            String duringStart = aprule.getDuringStart();
            // 24小时费用上限(单位分) 公共部分
            int highestFeeOneDay = aprule.getHighestFeeOneDay();
            // 在场时间或首x分钟
            String inParkTime = aprule.getInParkTime();
            // 首x分钟的费用(单位分)
            int inParkTimeCost = aprule.getInParkTimeCost();
            // 项目编码
            String parkIdStr = aprule.getParkId();
            // 规则编码
            int parkTypeID = aprule.getParkTypeID();
            // 规则名称
            String parkTypeName = aprule.getParkTypeName();
            // 单位时间的费用(单位元)
            int perCost = aprule.getPerCost();
            // 单位时间(分钟) 公共部分
            int perTime = aprule.getPerTime();

            TbParkingrule tbParkingrule = new TbParkingrule();
            tbParkingrule.setId(tbParkingruleMapper.nextSeq());
            tbParkingrule.setBusyName(StringUtils.isBlank(busyID) ? -1 : Integer.valueOf(busyID));
            tbParkingrule.setCarTypeCode(carTypeCode);
            tbParkingrule.setCreate_date(new Date());
            tbParkingrule.setDuringEnd(duringEnd);
            tbParkingrule.setDuringHighestFee(duringHighestFee);
            tbParkingrule.setDuringStart(duringStart);
            tbParkingrule.setHighestFeeOneDay(highestFeeOneDay);
            tbParkingrule.setInParkTime(StringUtils.isBlank(inParkTime) ? -1 : Integer.valueOf(inParkTime));
            tbParkingrule.setInParkTimeCost(inParkTimeCost);
            tbParkingrule.setName(parkTypeName);
            tbParkingrule.setParkID(park.getId());
            tbParkingrule.setPerCost(perCost);
            tbParkingrule.setPerTime(perTime);
            tbParkingrule.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
            tbParkingrule.setUpdate_date(new Date());

            tbParkRulelist.add(tbParkingrule);
        }

        // 保存车场规则列表
        parkBiz.syncSave(tbParkRulelist, park);

    }

}
